import classNames from 'classnames'
import './index.scss'
import {useMemo, useState} from "react";
import {billTypeToName} from "@/dictionary/index.jsx";
import Icon from "@/components/Icon.jsx";

const DailyBill = ({day, billList}) => {
    const dayResult = useMemo(() => {
        const pay = billList.reduce((pre, item) => {
            return item.type === 'pay' ? pre + item.money : pre
        }, 0)
        const income = billList.reduce((pre, item) => {
            return item.type === 'income' ? pre + item.money : pre
        }, 0)
        return {
            pay,
            income,
            total: pay + income
        }
    }, [billList])

    //控制细节显示
    const [visible, setVisible] = useState(false)
    return (
        <div className={classNames('dailyBill')}>
            <div className="header">
                <div className="dateIcon">
                    <span className="date">{day}</span>
                    <span className={classNames('arrow', visible && 'expand')} onClick={() => setVisible(!visible)}></span>
                </div>
                <div className="oneLineOverview">
                    <div className="pay">
                        <span className="type">支出</span>
                        <span className="money">{-dayResult.pay}</span>
                    </div>
                    <div className="income">
                        <span className="type">收入</span>
                        <span className="money">{
                            dayResult.income
                        }</span>
                    </div>
                    <div className="balance">
                        <span className="money">{dayResult.total}</span>
                        <span className="type">结余</span>
                    </div>
                </div>
            </div>
            <div className={classNames('billList', !visible && 'hidden')}>
                {billList.map(item => {
                    return (
                        <div className="bill" key={item.id}>
                            <div className="detail">
                                <div className="billType"><Icon type={item.useFor}></Icon>{billTypeToName[item.useFor]}</div>
                            </div>
                            <div className={classNames('money', item.type)}>
                                {item.money.toFixed(2)}
                            </div>
                        </div>
                    )
                })}
            </div>
        </div>
    )
}
export default DailyBill